package cn.datawin.cmppserver.netway.handler;

import cn.datawin.sms.cmpp2.ReportNotify;
import com.drondea.sms.message.IMessage;
import com.drondea.sms.message.cmpp.CmppDeliverResponseMessage;
import com.drondea.sms.type.IMessageResponseHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * 自定义回执处理器
 *
 * @author liuyanning
 */
@Slf4j
public class ReportResponseCustomHandler implements IMessageResponseHandler {

    private ReportNotify reportNotify;

    public ReportResponseCustomHandler(ReportNotify reportNotify) {
        this.reportNotify = reportNotify;
    }


    @Override
    public void messageComplete(IMessage msgRequest, IMessage msgResponse) {
        setResponseInfo(reportNotify, msgRequest, msgResponse);
        reportNotify.setSubmitDate(new Date(msgRequest.getSendTimeStamp()));
        reportNotify.setResponseDate(new Date());
        reportNotify.setStatus(0);
        log.debug("reportNotify,{}", reportNotify);
//        SpringContextUtil.getBean(ProducerService.class).sendDbMessage(reportNotify);
    }

    @Override
    public void messageExpired(String key, IMessage msgRequest) {
        reportNotify.setSubmitDate(new Date(msgRequest.getSendTimeStamp()));
        reportNotify.setStatus(2);
        log.debug("reportNotify,{}", reportNotify);
//        SpringContextUtil.getBean(ProducerService.class).sendDbMessage(reportNotify);
    }

    @Override
    public void sendMessageFailed(IMessage msgRequest) {
        reportNotify.setSubmitDate(new Date(msgRequest.getSendTimeStamp()));
        reportNotify.setStatus(1);
        log.debug("reportNotify,{}", reportNotify);
//        SpringContextUtil.getBean(ProducerService.class).sendDbMessage(reportNotify);
    }

    public void setResponseInfo(ReportNotify reportNotify, IMessage msgRequest, IMessage msgResponse) {
        CmppDeliverResponseMessage responseMessage = (CmppDeliverResponseMessage) msgResponse;
        reportNotify.setMsgId(responseMessage.getMsgId().toString());
        reportNotify.setSequence(responseMessage.getSequenceId());
        reportNotify.setResult(responseMessage.getResult());
    }
}
